/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package onlinebuy;

/**
 *
 * @author Yang Wu
 */

import java.sql.*;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//bulid GUI

public class Order {

private JFrame f2;
private JButton b;
private JLabel l1,l2,l3,l4;
private JTextField tf1,tf2,tf3;
private JPasswordField pf1;
private static String password = "1111";

public Order(){
    f2=new JFrame("Order");
    f2.setLayout(null);
    l1=new JLabel("Iterm Description");
    l2=new JLabel("Username");
    l3=new JLabel("Password");
    l4=new JLabel("Quantity of goods");
    tf1=new JTextField();
    tf2=new JTextField("aaa");
    tf3=new JTextField("1");
    pf1=new JPasswordField(4);
    pf1.setEchoChar('*');
    b=new JButton("Confirm");




    f2.add(l1);f2.add(tf1);f2.add(tf2);
    f2.add(pf1);f2.add(tf3);f2.add(b);
    f2.add(l2);f2.add(l3);f2.add(l4);

    Insets insets = f2.getInsets();
    Dimension size = l1.getPreferredSize();
    l1.setBounds(0 + insets.left, insets.top,
             200+size.width, 600+size.height);
    size = tf1.getPreferredSize();
    tf1.setBounds(260 + insets.left, 0 + insets.top,
             600+size.width,200+ size.height);

    size = l2.getPreferredSize();
    l2.setBounds(260 + insets.left, 210+insets.top,
             size.width + 20, size.height + 20);

    size = l3.getPreferredSize();
    l3.setBounds(550 + insets.left, 210+insets.top,
             size.width + 20, size.height + 20);

    size = tf2.getPreferredSize();
    tf2.setBounds(260 + insets.left, 250+insets.top,
             size.width + 200, size.height + 20);

    size = pf1.getPreferredSize();
    pf1.setBounds(550 + insets.left, 250+insets.top,
             size.width + 200, size.height + 20);
    size = b.getPreferredSize();
    b.setBounds(260 + insets.left, 350+insets.top,
             size.width + 200, size.height + 20);
    size = tf3.getPreferredSize();
    tf3.setBounds(550 + insets.left, 350+insets.top,
             size.width + 200, size.height + 20);

    size = l4.getPreferredSize();
    l4.setBounds(550 + insets.left, 300+insets.top,
             size.width + 20, size.height + 20);

      orderhandler hl=new orderhandler();
    
      b.addActionListener(hl);
      pf1.addActionListener(hl);
      
     
    f2.setSize(800,600);
    f2.setVisible(true);

}

//handle the action of GUI

    private class orderhandler implements ActionListener{


        public void actionPerformed(ActionEvent e) {
             if(e.getSource()==b){
               try {
            Class.forName("com.mysql.jdbc.Driver");

            Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineshop","root","121121");
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("SELECT username,id FROM customers WHERE password ='1111' LIMIT 2");
            while(rs.next()){
                int nbr = rs.getInt("id");
                String name = rs.getString("username");
                System.out.println("username: " + name + " and id " + nbr);
                JOptionPane.showMessageDialog(null,"Dear "+name+" you order have been dispatched", "Message", JOptionPane.WARNING_MESSAGE);
            }
            c.close();
        } catch (Exception ex) {
            System.err.println("ERROR: " + ex);
        }
             
            }
        
           JPasswordField input=(JPasswordField) e.getSource();
           char[] ch=input.getPassword();
           String p=new String(ch);

           if(p.equals(password)){
               JOptionPane.showMessageDialog(null,"Correct Password");
           }else{
               JOptionPane.showMessageDialog(null, "Invalid Password");
           }

        }

}
     
}
